package com.flightgame.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class AirportDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public void addAirport(Airport airport){
		try{
			sessionFactory.getCurrentSession().persist(airport);
			}
		catch(Exception sq){
			sq.printStackTrace();
		}
	}
	
    @SuppressWarnings("unchecked")
	public List<Airport> getAirports(){
		return sessionFactory.getCurrentSession().createQuery("from Airport").list();
	}
    
    @SuppressWarnings("unchecked")
    public List<Airport> getAirportsOnBoundedCoordinates(double lat1,double lat2,double lon1,double lon2){
    	return sessionFactory.getCurrentSession().createQuery("from Airport where  lat > :lat1 AND lat < :lat2 AND lon > :lon1 AND lon < :lon2").setDouble("lat1",lat1).setDouble("lat2",lat2).setDouble("lon1",lon1).setDouble("lon2",lon2).list();
    }
    
    public Airport getAirportByCode(String code){
    	return (Airport) sessionFactory.getCurrentSession().createQuery("from Airport where airportcode=:airportcode").setParameter("airportcode",code).uniqueResult();
    }
}
